/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* @author  Rezan Achmad
 *          13508104
 * BuildMap.java
 *
 * Created on May 8, 2010, 7:24:43 AM
 */

package View.BuildMode;

import Model.Building.Barrack;
import Model.Building.Blacksmith;
import Model.Building.Building;
import Model.Building.Castle;
import Model.Map.Map;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.plaf.basic.BasicBorders.FieldBorder;
import Support.*;
import View.MainMenu;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.Point;
import java.awt.event.KeyListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author user
 */
public class BuildMap extends javax.swing.JFrame implements MouseListener, MouseMotionListener, KeyListener, ActionListener{

    /** Creates new form BuildMap */
    public BuildMap(MainMenu parent, GraphicsDevice device) {
        super(device.getDefaultConfiguration());
        this.device = device;
        this.parent = parent; /* Pointed parent Window */
        initComponents();
        ShowWindow();

        /* Initial Map */
        mapLogic = new Map(2); /* Create map by default */
        paintMap(); /* Paint Map */

        /* Add key listener */
        this.addKeyListener(this);

        /* Add Mouse Listener */
        terrainPanel.addMouseListener(this);
        terrainPanel.addMouseMotionListener(this);

        /* Add Action Listener */
        rumputButton.addActionListener(this);
        airButton.addActionListener(this);
        lumpurButton.addActionListener(this);
        pohonButton.addActionListener(this);
        castleButton.addActionListener(this);
        barrackButton.addActionListener(this);
        blacksmithButton.addActionListener(this);

        /* Set Action Command Name */
        rumputButton.setActionCommand(Map.GetString(Map.RUMPUT));
        airButton.setActionCommand(Map.GetString(Map.AIR));
        lumpurButton.setActionCommand(Map.GetString(Map.LUMPUR));
        pohonButton.setActionCommand(Map.GetString(Map.POHON));

        castleButton.setActionCommand(Building.CASTLE_S);
        barrackButton.setActionCommand(Building.BARRACK_S);
        blacksmithButton.setActionCommand(Building.BLACKSMITH_S);

        /* Init save panel */
        save = new SaveMapPanel(this);
        mainLayerPane.add(save);
        mainLayerPane.setLayer(save, MENU_LAYER);
        int width = save.getPreferredSize().width;
        int height = save.getPreferredSize().height;
        int x = (getWidth()-width)/2;
        int y = (getHeight()-height)/2;
        save.setBounds(x, y, width, height);
        save.setVisible(false);

        /* Init load panel */
        load = new LoadMapPanel(this);
        mainLayerPane.add(load);
        mainLayerPane.setLayer(load, MENU_LAYER);
        width = load.getPreferredSize().width;
        height = load.getPreferredSize().height;
        x = (getWidth()-width)/2;
        y = (getHeight()-height)/2;
        load.setBounds(x, y, width, height);
        load.setVisible(false);


        /* Init New Panel */
        New = new NewPanel(this);
        mainLayerPane.add(New);
        mainLayerPane.setLayer(New, MENU_LAYER);
        width = New.getPreferredSize().width;
        height = New.getPreferredSize().height;
        x = (getWidth()-width)/2;
        y = (getHeight()-height)/2;
        New.setBounds(x, y, width, height);
        New.setVisible(false);

        /* Init input player Panel */
        input = new InputPlayerPanel(this);
        getMainLayerPane().add(input);
        getMainLayerPane().setLayer(input, MENU_LAYER);
        width = input.getPreferredSize().width;
        height = input.getPreferredSize().height;
        x = (getWidth()-width)/2;
        y = (getHeight()-height)/2;
        input.setBounds(x, y, width, height);
        input.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainLayerPane = new javax.swing.JLayeredPane();
        MenuPanel = new javax.swing.JPanel();
        airButton = new javax.swing.JButton();
        pohonButton = new javax.swing.JButton();
        lumpurButton = new javax.swing.JButton();
        rumputButton = new javax.swing.JButton();
        castleButton = new javax.swing.JButton();
        blacksmithButton = new javax.swing.JButton();
        barrackButton = new javax.swing.JButton();
        selectAllButton = new javax.swing.JButton();
        deselectButton = new javax.swing.JButton();
        randomButton = new javax.swing.JButton();
        buttonPanel = new javax.swing.JPanel();
        saveButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        Load = new javax.swing.JButton();
        scroll = new javax.swing.JScrollPane();
        mapLayerPane = new javax.swing.JLayeredPane();
        terrainPanel = new javax.swing.JPanel();
        buildingPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Build Mode");
        setBackground(new java.awt.Color(0, 0, 0));
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(java.awt.Color.black);

        mainLayerPane.setBackground(new java.awt.Color(0, 0, 0));
        mainLayerPane.setOpaque(true);
        mainLayerPane.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                mainLayerPaneKeyPressed(evt);
            }
        });

        airButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/air.png"))); // NOI18N
        airButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        airButton.setMaximumSize(new java.awt.Dimension(60, 60));
        airButton.setMinimumSize(new java.awt.Dimension(60, 60));
        airButton.setOpaque(false);
        airButton.setPreferredSize(new java.awt.Dimension(60, 60));

        pohonButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/pohon.png"))); // NOI18N
        pohonButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        pohonButton.setMaximumSize(new java.awt.Dimension(60, 60));
        pohonButton.setMinimumSize(new java.awt.Dimension(60, 60));
        pohonButton.setOpaque(false);
        pohonButton.setPreferredSize(new java.awt.Dimension(60, 60));

        lumpurButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/lumpur.png"))); // NOI18N
        lumpurButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        lumpurButton.setMaximumSize(new java.awt.Dimension(60, 60));
        lumpurButton.setMinimumSize(new java.awt.Dimension(60, 60));
        lumpurButton.setOpaque(false);
        lumpurButton.setPreferredSize(new java.awt.Dimension(60, 60));

        rumputButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/rumput.png"))); // NOI18N
        rumputButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        rumputButton.setMaximumSize(new java.awt.Dimension(60, 60));
        rumputButton.setMinimumSize(new java.awt.Dimension(60, 60));
        rumputButton.setOpaque(false);
        rumputButton.setPreferredSize(new java.awt.Dimension(60, 60));

        castleButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/castle1.png"))); // NOI18N
        castleButton.setMaximumSize(new java.awt.Dimension(60, 60));
        castleButton.setMinimumSize(new java.awt.Dimension(60, 60));
        castleButton.setOpaque(false);
        castleButton.setPreferredSize(new java.awt.Dimension(60, 60));

        blacksmithButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/blacksmith1.png"))); // NOI18N
        blacksmithButton.setMaximumSize(new java.awt.Dimension(60, 60));
        blacksmithButton.setMinimumSize(new java.awt.Dimension(60, 60));
        blacksmithButton.setOpaque(false);
        blacksmithButton.setPreferredSize(new java.awt.Dimension(60, 60));

        barrackButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/barrack1.png"))); // NOI18N
        barrackButton.setMaximumSize(new java.awt.Dimension(60, 60));
        barrackButton.setMinimumSize(new java.awt.Dimension(60, 60));
        barrackButton.setOpaque(false);
        barrackButton.setPreferredSize(new java.awt.Dimension(60, 60));

        selectAllButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        selectAllButton.setText("Select All");
        selectAllButton.setOpaque(false);
        selectAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectAllButtonActionPerformed(evt);
            }
        });

        deselectButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        deselectButton.setText("Deselect");
        deselectButton.setOpaque(false);
        deselectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deselectButtonActionPerformed(evt);
            }
        });

        randomButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        randomButton.setText("Random");
        randomButton.setMaximumSize(new java.awt.Dimension(72, 23));
        randomButton.setMinimumSize(new java.awt.Dimension(72, 23));
        randomButton.setOpaque(false);
        randomButton.setPreferredSize(new java.awt.Dimension(72, 23));
        randomButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                randomButtonActionPerformed(evt);
            }
        });

        buttonPanel.setOpaque(false);

        saveButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        saveButton.setText("Save");
        saveButton.setOpaque(false);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        backButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        backButton.setText("Back");
        backButton.setOpaque(false);
        backButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                backButtonMouseClicked(evt);
            }
        });

        newButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        newButton.setText("New");
        newButton.setOpaque(false);
        newButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newButtonActionPerformed(evt);
            }
        });

        Load.setFont(new java.awt.Font("Tahoma", 0, 18));
        Load.setText("Load");
        Load.setOpaque(false);
        Load.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(saveButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                    .addComponent(Load, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                    .addComponent(newButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                    .addComponent(backButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
                .addContainerGap())
        );
        buttonPanelLayout.setVerticalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(newButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Load)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(backButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout MenuPanelLayout = new javax.swing.GroupLayout(MenuPanel);
        MenuPanel.setLayout(MenuPanelLayout);
        MenuPanelLayout.setHorizontalGroup(
            MenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, MenuPanelLayout.createSequentialGroup()
                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(MenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(MenuPanelLayout.createSequentialGroup()
                        .addComponent(airButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lumpurButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(castleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(barrackButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(MenuPanelLayout.createSequentialGroup()
                        .addComponent(pohonButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rumputButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(blacksmithButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(MenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(randomButton, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                    .addComponent(selectAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(deselectButton, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
                .addContainerGap())
        );
        MenuPanelLayout.setVerticalGroup(
            MenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MenuPanelLayout.createSequentialGroup()
                .addGroup(MenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(MenuPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(MenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(MenuPanelLayout.createSequentialGroup()
                                .addComponent(selectAllButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(deselectButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(randomButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(MenuPanelLayout.createSequentialGroup()
                                .addGroup(MenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(castleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lumpurButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(barrackButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(airButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(MenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(pohonButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(rumputButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(blacksmithButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        MenuPanel.setBounds(0, 360, -1, -1);
        mainLayerPane.add(MenuPanel, new Integer(1));

        scroll.setBackground(new java.awt.Color(0, 0, 0));

        mapLayerPane.setBackground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout terrainPanelLayout = new javax.swing.GroupLayout(terrainPanel);
        terrainPanel.setLayout(terrainPanelLayout);
        terrainPanelLayout.setHorizontalGroup(
            terrainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        terrainPanelLayout.setVerticalGroup(
            terrainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        terrainPanel.setBounds(0, 0, -1, -1);
        mapLayerPane.add(terrainPanel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        buildingPanel.setOpaque(false);

        javax.swing.GroupLayout buildingPanelLayout = new javax.swing.GroupLayout(buildingPanel);
        buildingPanel.setLayout(buildingPanelLayout);
        buildingPanelLayout.setHorizontalGroup(
            buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        buildingPanelLayout.setVerticalGroup(
            buildingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        buildingPanel.setBounds(170, 60, -1, -1);
        mapLayerPane.add(buildingPanel, new Integer(1));

        scroll.setViewportView(mapLayerPane);

        scroll.setBounds(0, 0, 680, 340);
        mainLayerPane.add(scroll, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mainLayerPane, javax.swing.GroupLayout.PREFERRED_SIZE, 711, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mainLayerPane, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void backButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_backButtonMouseClicked
        parent.setVisible(true) /* Show parent Window */;
        this.dispose(); /* Close this window */
    }//GEN-LAST:event_backButtonMouseClicked

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
        New.setVisible(true);
    }//GEN-LAST:event_newButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        /* Save Map */
        int i=1;
        while(mapLogic.IsCastleIn(i)==true && i<=mapLogic.GetNumPlayer()){
            ++i;
        }
        if(i<=mapLogic.GetNumPlayer()){
            JOptionPane.showConfirmDialog(this, "Castil untuk player belum diset semua. Save gagal");
        } else {
            save.setVisible(true);
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void LoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoadActionPerformed
        /* Create & Show LoadMap Window */
        load.setVisible(true);
    }//GEN-LAST:event_LoadActionPerformed

    private void selectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllButtonActionPerformed
        /* Get All Component in contentPanel */
        Component[] list = terrainPanel.getComponents();

        JLabel l;

        /* Clear All selected Label */
        selected.clear();

        /* Select All Map components */
        for (int i=0;i<list.length;++i) {
            l = (JLabel)list[i];
            l.setBorder(new FieldBorder(Color.BLACK, Color.BLACK, Color.BLACK, Color.BLACK)); /* Set border for Label l */

            /* Add l to selected List */
            selected.add(l);
        }
    }//GEN-LAST:event_selectAllButtonActionPerformed

    private void deselectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deselectButtonActionPerformed
        JLabel l;

        for (int i=0;i<selected.size();++i) {
            l = selected.get(i);
            l.setBorder(null); /* Erase Border */
        }

        /* Clear selected */
        selected.clear();
    }//GEN-LAST:event_deselectButtonActionPerformed

    private void randomButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_randomButtonActionPerformed
        /* Random */
        mapLogic.GetBuildings().removeAllElements();
        mapLogic.SetMapRandom();

        /* Repaint */
        paintMap();
    }//GEN-LAST:event_randomButtonActionPerformed

    private void mainLayerPaneKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_mainLayerPaneKeyPressed
        // TODO add your handling code here:
}//GEN-LAST:event_mainLayerPaneKeyPressed

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {
         /* Get Component at mouse click's position */
        
        Component c = terrainPanel.getComponentAt(e.getX(), e.getY());
        Component c2 = buildingPanel.getComponentAt(e.getX(), e.getY());
        /* Create temporary label*/
        JLabel l;
        JLabel l2 = null;
        
         if (c2 instanceof JLabel) {
            l2 = (JLabel)c2;
         }

        if (c instanceof JLabel) { /* Cek c is a Label */
             l = (JLabel)c; /* l point to c */

             /* Cek if l is selected */
            if (selected.contains(l)) { /* Cek is selected */
                l.setBorder(null); /* Erase border */
                selected.remove(l); /* Remove Label l from selected List */
//                if (l2!=null) {
//                    l2.setBorder(null);
//                }
            } else { /* Cek is not yet selected */
                FieldBorder b = new FieldBorder(Color.BLACK, Color.BLACK, Color.BLACK, Color.BLACK);
//                if (l2!=null) {
//                    l2.setBorder(b);
//                }
                l.setBorder(b); /* Set border for Label l */
                selected.add(l); /* Add Label l to selected List */
            }
        } else { /* c isn't a Label */
            return;
        }
    }

    public void mouseReleased(MouseEvent e) {}

    public void mouseDragged(MouseEvent e) {
        /* Get Label at mouse dragged's point */
        Component c = terrainPanel.getComponentAt(e.getX(), e.getY());
        JLabel l;

        if (c instanceof JLabel) {
            l = (JLabel)c;
            if (!selected.contains(l)) { /* l is not yet selected */
                l.setBorder(new FieldBorder(Color.BLACK, Color.BLACK, Color.BLACK, Color.BLACK)); /* Set border for Label l */
                selected.add(l);
            }
        }
    }

    public void mouseMoved(MouseEvent e) {}

    public void keyPressed(KeyEvent e) {
        System.out.println("press");
        System.out.println(e.getKeyChar());
        System.out.println(e);
    }

    public void keyReleased(KeyEvent e) {
        System.out.println("release");
        System.out.println(e.getKeyChar());
        System.out.println(e);
    }

    public void keyTyped(KeyEvent e) {
        System.out.println("type");
        System.out.println(e.getKeyChar());
        System.out.println(e);
    }

    public void actionPerformed(ActionEvent e) {
        JLabel l;
        Component c;
        String s = e.getActionCommand();
        /* Change image selected terrain or building and its Value in mapLogic */

        if (s.equals(Building.CASTLE_S) || s.equals(Building.BARRACK_S) || s.equals(Building.BLACKSMITH_S)) {
           input.update(mapLogic.GetNumPlayer(), s);
           input.setVisible(true);
        } else  {
            for (int i=0;i<selected.size();++i) {
                l = selected.get(i);
                l.setIcon(ImageSupport.createImageIcon(e.getActionCommand()+".png", null));

                Point gridpoint = Converter.PointToGrid(l.getX(), l.getY());
                /* remove building if any */
                c = buildingPanel.getComponentAt(l.getX(), l.getY());
                if (c instanceof JLabel) {
                    mapLogic.DelBuilding(gridpoint.x, gridpoint.y);
                    buildingPanel.remove(c);
                }
                /* chane terrain */
                mapLogic.SetTerrain(gridpoint.x, gridpoint.y, Map.GetTerrrainNumber(e.getActionCommand()));

                /* Remove border selected Label */
                l.setBorder(null);
            }
            buildingPanel.repaint();
            /* Remove All Label from selected List */
            selected.clear();
        }
    }

    @SuppressWarnings("static-access")
    public void UpdateBuilding(int player, String buildingname) {
        int n = buildingname.length();
        String s;
        JLabel l;
        if (selected.size()>0) {
            if (!(buildingname.equals("castle") && mapLogic.IsCastleIn(player))) {
                s = buildingname + Integer.toString(player);
                l = selected.get(0);
                createContent(l.getX(), l.getY(), ImageSupport.IMAGE_WIDTH, ImageSupport.IMAGE_HEIGHT, s, buildingPanel);
                buildingPanel.repaint();
                Point gridpoint = Converter.PointToGrid(l.getX(), l.getY());
                if (buildingname.equals(Building.CASTLE_S)) {
                    n = Map.CASTLE;
                }

                if (buildingname.equals(Building.BARRACK_S)) {
                    n = Map.BARRACK;
                }

                if (buildingname.equals(Building.BLACKSMITH_S)) {
                    n = Map.BLACKSMITH;
                }

                mapLogic.SetBuilding(n, player, gridpoint.x, gridpoint.y, 1, 0);
            }

            for (int i=0; i<selected.size(); ++i) {
                l = selected.get(i);
                l.setBorder(null);
            }
            selected.clear();
        }
    }

    public void ShowInputPlayer() {
        input.setVisible(true);
    }
    
    public void ShowWindow() {
        Container pane = getContentPane();
        int x, y, width, height;

        isFullScreen = device.isFullScreenSupported();
        setUndecorated(isFullScreen);
        setResizable(!isFullScreen);
//        if (isFullScreen) {
//            /* Full-screen mode */
//            device.setFullScreenWindow(this);
//            validate();
//        } else {
//            /* Windowed mode */
//            pack();
//            setVisible(true);
//        }
        width = device.getDisplayMode().getWidth();
        height = device.getDisplayMode().getHeight();
        setBounds(0, 0, width, height);
        setVisible(true);

        pane.setBackground(Color.BLACK);
        scroll.setBackground(Color.BLACK);
        mainLayerPane.setBackground(Color.BLACK);
        mapLayerPane.setBackground(Color.BLACK);
        mapLayerPane.setOpaque(true);
        
        /* add mainLayerPane to Frame */
        pane.removeAll();
        pane.setLayout(null);
        pane.add(getMainLayerPane());
        int border = BORDER_MAIN_LAYER_PANE;
        getMainLayerPane().setBounds(border, border, getWidth()-2*border, getHeight()-2*border);

        /* Set maniLayerPane's layout to null */
        getMainLayerPane().setLayout(null);

        /* Set Bounds scroll*/
        width = getMainLayerPane().getWidth();
        height = getMainLayerPane().getHeight();
        scroll.setBounds(0, 0, width, height);

        /* Set Bounds MenuPanel in mainLayerPane */
        width = MenuPanel.getPreferredSize().width;
        height = MenuPanel.getPreferredSize().height;
        x = (getMainLayerPane().getWidth() - width)/2;
        y = getMainLayerPane().getHeight() - height;
        MenuPanel.setBounds(x, y, width, height);
    }

    public void createContent(int x, int y, int width, int height, String iconName, JPanel p) {
        Component c =  p.getComponentAt(x, y);
        if (c!=null) {
            p.remove(c);
        }
        JLabel label;
        label = new JLabel(ImageSupport.createImageIcon(iconName+".png", null));
        label.setBounds(x, y, width, height);
        p.add(label);
    }

    public void paintMap() {
        /* Remove all component in content Panel */
        terrainPanel.removeAll();
        terrainPanel.repaint();
        buildingPanel.removeAll();
        buildingPanel.repaint();

        /* Add new component(terrain & building ) in content Panel */
        int x,y,width,height;
        String image;
        Building b;
        for (int i=0;i<mapLogic.GetWidth();++i) {
            for (int j=0;j<mapLogic.GetHeight();++j) {
                x = i*ImageSupport.IMAGE_WIDTH;
                y = j*ImageSupport.IMAGE_HEIGHT;
                width = ImageSupport.IMAGE_WIDTH;
                height = ImageSupport.IMAGE_HEIGHT;
                image = Map.GetString(mapLogic.GetTerrain(i, j));
                createContent(x, y, width, height, image, terrainPanel);
                b = mapLogic.GetBuilding(i, j);
                if (b!=null) {
                    String s=null;
                    if (b instanceof Castle) {
                        s = Building.CASTLE_S;
                    } else if (b instanceof Barrack) {
                        s = Building.BARRACK_S;
                    } else if (b instanceof Blacksmith) {
                        s = Building.BLACKSMITH_S;
                    }
                    s+=Integer.toString(b.getBuilding_BaseAtribut(b.BUILDING_PLAYER_IDX));
                    createContent(x, y, width, height, s, buildingPanel);
                }
            }
        }
        terrainPanel.repaint();
        buildingPanel.repaint();

        /* Set new Size for contentPanel */
        Dimension d = new Dimension(ImageSupport.IMAGE_WIDTH*mapLogic.GetWidth(), ImageSupport.IMAGE_HEIGHT*mapLogic.GetHeight());
        terrainPanel.setBounds(0, 0, d.width, d.height);
        buildingPanel.setBounds(0, 0, d.width, d.height);
        if (d.width<2000) {
            d = new Dimension(2000, 2000);
        }
        mapLayerPane.setPreferredSize(d);
    }

    public Map GetMap() {
        return mapLogic;
    }

    public void SetMap(Map map) {
        mapLogic = map;
    }

    public void SetMapName(String mapName) {
        this.mapName = mapName;
    }

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Load;
    private javax.swing.JPanel MenuPanel;
    private javax.swing.JButton airButton;
    private javax.swing.JButton backButton;
    private javax.swing.JButton barrackButton;
    private javax.swing.JButton blacksmithButton;
    private javax.swing.JPanel buildingPanel;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton castleButton;
    private javax.swing.JButton deselectButton;
    private javax.swing.JButton lumpurButton;
    private javax.swing.JLayeredPane mainLayerPane;
    private javax.swing.JLayeredPane mapLayerPane;
    private javax.swing.JButton newButton;
    private javax.swing.JButton pohonButton;
    private javax.swing.JButton randomButton;
    private javax.swing.JButton rumputButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JScrollPane scroll;
    private javax.swing.JButton selectAllButton;
    private javax.swing.JPanel terrainPanel;
    // End of variables declaration//GEN-END:variables

    private MainMenu parent;
    private GraphicsDevice device;
    private boolean isFullScreen;
    private ArrayList<JLabel> selected = new ArrayList<JLabel>();
    private Map mapLogic;
    private String mapName = null;
    private SaveMapPanel save;
    private LoadMapPanel load;
    private NewPanel New;
    private InputPlayerPanel input;
    private int playerbuilding;

    public static final int BORDER_MAIN_LAYER_PANE = 5;
    public static final int MAP_LAYER_PANE_LAYER = JLayeredPane.DEFAULT_LAYER;
    public static final int MENU_LAYER = JLayeredPane.DEFAULT_LAYER+1;
    public static final int TERRAIN_LAYER = JLayeredPane.DEFAULT_LAYER;
    public static final int BUILDING_LAYER = JLayeredPane.DEFAULT_LAYER+1;

    /**
     * @return the mainLayerPane
     */
    public javax.swing.JLayeredPane getMainLayerPane() {
        return mainLayerPane;
    }

    /**
     * @param playerbuilding the playerbuilding to set
     */
    public void setPlayerbuilding(int playerbuilding) {
        this.playerbuilding = playerbuilding;
    }
}
